package com.green.web.controller;

import com.green.web.utils.FileUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FileController {


    /**
     * 单文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("sign")
    public String signUpload(@RequestParam("file") MultipartFile file) {
        System.out.println("文件名称:" + file.getName());
        return "成功";
    }


    @PostMapping("multi")
    public String multiUpload(@RequestParam("file") MultipartFile[] file) {
        for (MultipartFile multipartFile : file) {
            System.out.println("文件名称:" + multipartFile.getName());
        }
        return "成功";
    }



    /**
     * 上传到本地
     *
     * @param file
     * @return
     */
    @RequestMapping("sign_upload_local")
    public String signUploadLocal(@RequestParam("file") MultipartFile file)  {
        System.out.println("文件名称:" + file.getName());
        try {
            String upload = FileUtils.upload(file);
            return upload;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
